/**
 * 
 */
define(function(require) {
	var comm = require('sdk/server');require('sdk/common');
	var userData = comm.getUser();
    var extParam = JSON.parse(appcan.ls.val('crossParam'));
    if(!extParam){
        return console.log('参数错误')
    };
    var mark;
    if(extParam && extParam['mark']){
        mark = extParam['mark'];
    };
    var choosenItem;
    var dataTemp = $('#storeListTemp').val();
    var dataRender = function(data, getMore) {
        $.each(data.data,function(i,e){
            if(e['sublist'] && e.sublist.length){
                e.hasSub = 1;
            };
            //评分
            if(e['generalScore']){
                e.note = '综合评分：'+e['generalScore']
            };
        });
        extParam = data;
        //console.log(extParam)
        var render = etpl.compile(dataTemp);
        var html = render(data);
        if (getMore) {
            $('#storeList').append(html);
        } else {
            $('#storeList').html(html);
        }
        $('.servState ._score').each(function(i, e) {
            var score = parseFloat($(e).text());
            $(e).parent().find('._stars').raty({
                score: score,
                readOnly: true
            });
        });
        appcan.window.evaluateScript('','appcan.loading.hide()');
    };

    var getData = function(getMore) {
        appcan.ajax({
            url: appcfg.host.control + extParam.url,
            data: $.extend({
                member_id: userData.id,
                sid: appcfg.project.sid
            }, extParam),
            offline :true,
            expires: 2592000000,
            success: function(res) {    
                if (res.status === 'Y') {
                    dataRender(res, getMore);
                } else {
                	appcan.loading.hide();
                    $('#storeList').empty();
                    $.box.msg(res.message, {
                        color: 'danger',
                        delay: 2000
                    });
                }
            },
            error:function(){
                appcan.window.openToast('网络异常，请重试');
                appcan.window.evaluateScript('','appcan.loading.hide()');
            }
        });
    }
    //匹配下一级array
    var getListById = function(id,oArray){
        var _;
        if($.isArray(oArray)){
            $.each(oArray,function(i,e){
                if(e.id==id){
                    if(e.sublist){
                        return _ = e.sublist
                    }else{
                        return console.warn('缺少sublist字段!');
                    }
                }
            });
        }
        return _;
    };
    var init = function(){
        if(extParam && extParam['url']){
            getData();
        }else{
            dataRender(extParam);
        }
    }
    //点选
    $('#storeList').on('touchstart', '.chooseListHook', function() {
        //单选
        $(this).addClass('active');
    }).on('touchend', '.chooseListHook',function(){
        if($(this).hasClass('active')){
            $(this).removeClass('active');
            choosenItem = {};
            if(mark){
                choosenItem.mark = mark;
            }
            choosenItem.id=$(this).data('id');
            choosenItem.name=$(this).data('name');
            appcan.ls.val('choosenItem',JSON.stringify(choosenItem));
            appcan.window.evaluateScript('','submitChoose()');
        }
        
    }).on('touchmove', '.chooseListHook',function(){
        $(this).removeClass('active');
    })
    .on('touchcancel', '.chooseListHook',function(){
        $(this).removeClass('active');
    }).on('click','.chooseListHookMulti',function(){
        //多选

    }).on('click','.item-icon-right',function(){
        //下级
        choosenItem = null;
        var subList = getListById($(this).data('id'), extParam.data);
        extParam.data  = subList;
        init();
    });

	appcan.ready(function() {
		appcan.window.enableBounce();
		init();

	});
});